﻿using AJNetDll;
using BLL;
using DBUtility;
using gfLibrary;
using Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using UtilityHelper;

namespace IYAKASAPortal.Login
{
    public partial class LoginRoadResult : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDropDownList();
            }
        }


        /// <summary>
        /// 返回页面填写的数据
        /// </summary>
        /// <param name="list">选择的R1、R2值</param>
        /// <param name="succ">数据验证</param>
        /// <param name="updatePanel"></param>
        /// <returns></returns>
        public RESULT_ROAD GetRoadResultEntity(out bool succ,VEHICLE_DISPATCH ddEntity,out string Msg)
        {
            Msg = string.Empty;

            RESULT_ROAD entity = new RESULT_ROAD();

            string strLSJCCS = ddEntity.JCCS.ToString();
            string strLSZDCSD = txtLSZDCSD.Text.Trim();
            string strLSZDPPQK = rblLSZDPPQK.SelectedValue;
            string strLSZDJL = txtLSZDJL.Text.Trim();
            string strMFDD = txtMFDD.Text.Trim();
            string strLSZDXTSJ = txtLSZDXTSJ.Text.Trim();
            string strLSZDSCDK = txtLSZDSCDK.Text.Trim();
            string strLSZD_PD = rblLSZDPD.SelectedValue;

            string strLSZCPD = rblLSZCPD.SelectedValue;
            string strLSZCQK = rblLSZCQK.SelectedValue;
            //string strZFXTCSJ = txtZFXTCBC.Text.Trim();
            string strLSJYY = drpLSJYY.SelectedValue;


            #region 数据完整性判断
            succ = true;
            //if (ddEntity.JYXM.Contains("R1") || ddEntity.JYXM.Contains("R2"))
            //{
            //    if ( !string.IsNullOrEmpty(strLSJCCS) &&  gfValidate.Is_Integer(strLSJCCS) == false)
            //    {
            //        succ = false;
                    
            //        Msg = "[路试检测次数]不是有效的数字格式。";
            //        return entity;

            //    }
            //}
            if (ddEntity.JYXM.Contains("R1"))
            {
                if (!string.IsNullOrEmpty(strLSZDCSD) && gfValidate.Is_Float(strLSZDCSD) == false)
                {
                    succ = false;
                   
                    Msg = "[路试制动初速度]不是有效的数字格式。";
                    return entity;
                }

                if (!string.IsNullOrEmpty(strLSZDJL) && gfValidate.Is_Float(strLSZDJL) == false)
                {
                    succ = false;
                   
                    Msg = "[路试制动距离]不是有效的数字格式。";
                    return entity;
                }

                if (!string.IsNullOrEmpty(strMFDD) && gfValidate.Is_Float(strMFDD) == false)
                {
                    succ = false;
                    
                    Msg = "[MFDD数据]不是有效的数字格式。";
                    return entity;
                }
                if (!string.IsNullOrEmpty(strLSZDXTSJ) && gfValidate.Is_Float(strLSZDXTSJ) == false)
                {
                    succ = false;
                   
                    Msg = "[路试制动协调时间]不是有效的数字格式。";
                    return entity;
                }
                if (!string.IsNullOrEmpty(strLSZDSCDK) && gfValidate.Is_Float(strLSZDSCDK) == false)
                {
                    succ = false;
                   
                    Msg = "[路试制动试车道宽]不是有效的数字格式。";
                    return entity;
                }


            }
            #endregion

            entity.LSJCCS = strLSJCCS;
            entity.LSZDCSD = strLSZDCSD;
            entity.LSZDPPQK = strLSZDPPQK;
            entity.LSZDJL = strLSZDJL;
            entity.MFDD = strMFDD;
            entity.LSZDXTSJ = strLSZDXTSJ;
            entity.LSZDSCDK = strLSZDSCDK;
            entity.LSZD_PD = strLSZD_PD;

            entity.LSZCPD = strLSZCPD;
            entity.LSZC_PD = strLSZCQK;
            entity.LSJYY = strLSJYY;

            return entity;
        }


        /// <summary>
        /// 提交路试信息
        /// </summary>
        /// <param name="entity">路试实体</param>
        /// <param name="ddEntity">调度表实体</param>
        /// <param name="strJKID">接口ID</param>
        /// <param name="strJCLSH">检测流水号</param>
        /// <param name="jyxmList">检验项目</param>
        /// <param name="Message"></param>
        /// <returns></returns>
        public bool UploadRoadResult(RESULT_ROAD entity, VEHICLE_DISPATCH ddEntity, out string Message)
        {
            bool succ = false;

            string jyxm = string.Empty;

            if (ddEntity.JYXM.Contains("R1"))
            {
                jyxm += "R1,";
            }

            if (ddEntity.JYXM.Contains("R2"))
            {
                jyxm += "R2,";
            }

            succ = StartRoad(entity, ddEntity, out Message);
            if (succ)
            {
                succ = WriteRoad(entity, ddEntity, "18C54", jyxm, out Message);
                if (succ)
                {
                    WriteFinish(entity, ddEntity, "18C58");
                    entity.LSJYY = ddEntity.YCY;
                    SaveRoadResult(entity, ddEntity.JCLSH);
                }
            }
            return succ;
        }

        /// <summary>
        /// 保存路试信息
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="strJCLSH">检测流水号</param>
        private bool SaveRoadResult(RESULT_ROAD entity,string strJCLSH)
        {
            bool succ = false;
            RESULT_ROAD_BLL m_RESULT_ROAD_BLL = new RESULT_ROAD_BLL();
            if (entity.LSZD_PD == "1")
            {
                entity.MFDD_PD = "1";
                entity.LSZDJL_PD = "1";
                entity.LSZDXTSJ_PD = "1";
            }
            else
            {
                entity.MFDD_PD = "2";
                entity.LSZDJL_PD = "2";
                entity.LSZDXTSJ_PD = "2";
            }

            if (m_RESULT_ROAD_BLL.Exists(strJCLSH) == false)
            {
                entity.JCLSH = strJCLSH;
                succ = m_RESULT_ROAD_BLL.Insert(entity);
            }
            else
            {
               entity.JCLSH = strJCLSH;
               succ = m_RESULT_ROAD_BLL.Update(entity, strJCLSH);
            }
            return succ;
        }


        #region 路试联网接口


        private bool StartRoad(RESULT_ROAD entity,VEHICLE_DISPATCH ddEntity,out string Message)
        {
            bool bWrite = false;
            Message = string.Empty;
            Networking netWorking = Networking.Instance;
            WriteCondition writeConditon = new WriteCondition();
            WriteVehicle writeVehicle = new WriteVehicle();
            Dictionary<string, string> dicPara = new Dictionary<string, string>();
            if (netWorking.bSFLW)
            {
                dicPara.Add("jkxlh", netWorking.jkxlh);
                dicPara.Add("xtlb", netWorking.xtlb);
                dicPara.Add("jylsh", ddEntity.AJLSH);
                dicPara.Add("jyjgbh", netWorking.jyjgbh);
                dicPara.Add("jkid", "18C55");
                dicPara.Add("jycs", entity.LSJCCS);
                dicPara.Add("hpzl", ddEntity.HPZLDH);
                dicPara.Add("hphm", ddEntity.HPHM);
                dicPara.Add("clsbdh",ddEntity.VIN);
                dicPara.Add("jyxm", "R");
                dicPara.Add("kssj", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                writeConditon.DicWritePara = dicPara;
                var vRetInfo = writeVehicle.WriteByJKId(writeConditon);
                bWrite = vRetInfo.IsSucessful;
                Message = vRetInfo.Message;
            }
            return bWrite;
        }

        /// <summary>
        /// 写入路试过程信息
        /// </summary>
        /// <param name="entity">路试结果实体</param>
        /// <param name="strJKID">路试接口id</param>
        /// <param name="Message">联网信息</param>
        /// <param name="ddEntity">调度表实体</param>
        /// <param name="jyxmList"></param>
        /// <returns></returns>
        private bool WriteRoad(RESULT_ROAD entity, VEHICLE_DISPATCH ddEntity, string strJKID, string jyxm, out string Message )
        {
            string str = jyxm;
            bool bWrite = false;
            #region 定义联网
            Networking netWorking = Networking.Instance;
            WriteCondition writeConditon = new WriteCondition();
            WriteVehicle writeVehicle = new WriteVehicle();
            Dictionary<string, string> dicPara = new Dictionary<string, string>();

            Message = string.Empty;
            if (netWorking.bSFLW)
            {
                dicPara.Add("jkxlh", netWorking.jkxlh);
                dicPara.Add("xtlb", netWorking.xtlb);
                dicPara.Add("jkid", strJKID);
                dicPara.Add("jylsh", ddEntity.AJLSH);
                dicPara.Add("jyjgbh", netWorking.jyjgbh);
                dicPara.Add("jcxdh", Convert.ToString(ddEntity.JCXH));
                dicPara.Add("jycs", entity.LSJCCS);
                dicPara.Add("jyxm", str);
                dicPara.Add("hpzl", ddEntity.HPZLDH);

                dicPara.Add("hphm", ddEntity.HPHM.Replace("挂", ""));
                dicPara.Add("clsbdh", ddEntity.VIN);

                if (ddEntity.JYXM.Contains("B0,") || ddEntity.JYXM.Contains("B1,") || ddEntity.JYXM.Contains("B2,"))
                {
                    dicPara.Add("lsy", ddEntity.YCY);
                }
                else
                {
                    dicPara.Add("lsy", entity.LSJYY);
                }


                dicPara.Add("zdcsd", entity.LSZDCSD);
                dicPara.Add("zdxtsj", entity.LSZDXTSJ);
                dicPara.Add("zdwdx", entity.LSZDPPQK);
                dicPara.Add("xckzzdjl", entity.LSZDJL);
                dicPara.Add("xcmzzdjl", string.Empty);
                dicPara.Add("xckzmfdd", entity.MFDD);
                dicPara.Add("xcmzmfdd", string.Empty);
                dicPara.Add("xczdczlz", string.Empty);
                dicPara.Add("lszdpd", entity.LSZD_PD);
                dicPara.Add("yjzdcsd", string.Empty);
                dicPara.Add("yjkzzdjl", string.Empty);
                dicPara.Add("yjkzmfdd", string.Empty);
                dicPara.Add("yjmzzdjl", string.Empty);
                dicPara.Add("yjmzmfdd", string.Empty);
                dicPara.Add("yjzdczlfs", string.Empty);
                dicPara.Add("yjzdczlz", string.Empty);
                dicPara.Add("yjzdpd", string.Empty);
                dicPara.Add("csdscz", string.Empty);
                dicPara.Add("csbpd", string.Empty);
                if (str.Contains("R2"))
                {

                    dicPara.Add("zcpd", entity.LSZCPD);

                    dicPara.Add("lszczdpd", entity.LSZC_PD);
                    if (Convert.ToInt32(entity.LSZD_PD) == 2 || Convert.ToInt32(entity.LSZC_PD) == 2)
                    {
                        dicPara.Add("lsjg", "2");
                    }
                    else if (Convert.ToInt32(entity.LSZC_PD) == 1 && Convert.ToInt32(entity.LSZD_PD) == 1)
                    {
                        dicPara.Add("lsjg", "1");
                    }
                    else
                    {
                        dicPara.Add("lsjg", "0");
                    }
                }
                else
                {
                    dicPara.Add("zcpd", string.Empty);
                    dicPara.Add("lszczdpd", string.Empty);
                    if (Convert.ToInt32(entity.LSZD_PD) == 2)
                    {
                        dicPara.Add("lsjg", "2");
                    }
                    else if (Convert.ToInt32(entity.LSZD_PD) == 1)
                    {
                        dicPara.Add("lsjg", "1");
                    }
                    else
                    {
                        dicPara.Add("lsjg", "0");
                    }
                }
                try
                {
                    writeConditon.DicWritePara = dicPara;
                    var vRetInfo = writeVehicle.WriteByJKId(writeConditon);
                    bWrite = vRetInfo.IsSucessful;
                    Message = vRetInfo.Message;
                }
                catch (Exception ex)
                {
                    Message = ex.Message;
                }
            }
            else
            {
                bWrite = true;
            }
            return bWrite;
            #endregion
        }

       /// <summary>
       /// 写入路试项目结束信息
       /// </summary>
       /// <param name="entity"></param>
       /// <param name="strJKID"></param>
       /// <param name="ddEntity"></param>
       /// <param name="jyxmList"></param>
        private void WriteFinish(RESULT_ROAD entity, VEHICLE_DISPATCH ddEntity, string strJKID)
        {
            Networking netWorking = Networking.Instance;
            WriteCondition writeConditon = new WriteCondition();
            WriteVehicle writeVehicle = new WriteVehicle();
            Dictionary<string, string> dicPara = new Dictionary<string, string>();
            if (netWorking.bSFLW)
            {
                dicPara.Add("jkxlh", netWorking.jkxlh);
                dicPara.Add("xtlb", netWorking.xtlb);
                dicPara.Add("jyjgbh", netWorking.jyjgbh);
                dicPara.Add("jkid", strJKID);
                dicPara.Add("jylsh", ddEntity.AJLSH);
                dicPara.Add("jcxdh", Convert.ToString(ddEntity.JCXH));
                dicPara.Add("jycs", entity.LSJCCS);
                dicPara.Add("jyxm", "R");
                dicPara.Add("hpzl", ddEntity.HPZLDH);
                dicPara.Add("hphm", ddEntity.HPHM.Replace("挂", ""));
                dicPara.Add("clsbdh", ddEntity.VIN);
                dicPara.Add("gwjysbbh", string.Empty);
                dicPara.Add("jssj", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                writeConditon.DicWritePara = dicPara;
                var vRetInfo = writeVehicle.WriteByJKId(writeConditon);
            }
        }
        #endregion


        private DataTable GetJYXTable()
        {
            DataTable dt = new DataTable();
            String strSQL = "SELECT ID,EMPLOYEE_NAME FROM EMPLOYEE_USER WHERE  LICENSE_NAME like '%10,%'";
            DbHelper.GetTable(strSQL, ref dt);
            return dt;
        }

        /// <summary>
        /// 绑定路试检验员列表
        /// </summary>
        private void BindDropDownList()
        {
            drpLSJYY.DataSource = GetJYXTable();
            drpLSJYY.DataValueField = "EMPLOYEE_NAME";
            drpLSJYY.DataTextField = "EMPLOYEE_NAME";
            drpLSJYY.DataBind();
        }
    }


}